//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.qzsoft.jeemis.platform.security.service.impl;

import com.qzsoft.jeemis.common.cacheutils.CacheGenUtils;
import com.qzsoft.jeemis.common.enums.SuperAdminEnum;
import com.qzsoft.jeemis.common.utils.ConvertUtils;
import com.qzsoft.jeemis.platform.dept.dto.BasicDeptDTO;
import com.qzsoft.jeemis.platform.security.service.BasicDeptService;
import com.qzsoft.jeemis.platform.security.service.BasicUserDeptService;
//import com.qzsoft.jeemis.platform.security.service.BasicUserRoleBusinessService;
import com.qzsoft.jeemis.platform.security.service.BasicUserTokenService;
import com.qzsoft.jeemis.platform.security.service.ShiroService;
//import com.qzsoft.jeemis.platform.security.user.RoleBusiness;
import com.qzsoft.jeemis.platform.security.user.UserCompany;
import com.qzsoft.jeemis.platform.security.user.UserDetail;
import com.qzsoft.jeemis.repository.sys.entity.BasicUserEntity;
import com.qzsoft.jeemis.repository.sys.entity.BasicUserTokenEntity;
import com.qzsoft.system.modules.permission.dept.service.SysDeptService;
import com.qzsoft.system.modules.sysmenu.service.SysMenuService;
import com.qzsoft.system.modules.zhjdsysdept.entity.ZhjdSysDeptEntity;
import com.qzsoft.system.modules.zhjdsysdept.service.ZhjdSysDeptService;
import com.qzsoft.system.modules.zhjdsysdept.utils.ZhjdSysDeptConstants;
import com.qzsoft.system.repository.sys.entity.SysDeptEntity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

@Service
public class ShiroServiceImpl implements ShiroService {

//    @Autowired
//    private PermissionService permissionService;
    
    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private BasicUserTokenService basicUserTokenService;

    @Autowired
    private BasicDeptService basicDeptService;

    @Autowired
    private BasicUserDeptService basicUserDeptService;

//    @Autowired
//    private BasicUserRoleBusinessService basicUserRoleBusinessService;

    @Autowired
    private SysDeptService sysDeptService;

    @Lazy
    @Autowired
    private ZhjdSysDeptService zhjdSysDeptService;

    public ShiroServiceImpl() {
    }

    public Set<String> getUserPermissions(UserDetail user) {
        List<String> permissionsList;
        if (user.getSuperAdmin() == SuperAdminEnum.YES.value()) {
//            permissionsList = this.permissionService.getPermissionsList();
            permissionsList = this.sysMenuService.getPermissionsList();
        } else {
//            permissionsList = this.permissionService.getUserPermissionsList(user.getId());
            permissionsList = this.sysMenuService.getUserPermissionsList(user.getId());
        }

        Set<String> permsSet = new HashSet<String>();
        Iterator<String> var4 = permissionsList.iterator();

        while (var4.hasNext()) {
            String permissions = (String) var4.next();
            if (!StringUtils.isBlank(permissions)) {
                permsSet.addAll(Arrays.asList(permissions.trim().split(",")));
            }
        }
        return permsSet;
    }

    public BasicUserTokenEntity getByToken(String token) {
        Object obj = CacheGenUtils.get("SYS_USER_TOKEN" + token);
        BasicUserTokenEntity basicUserTokenEntity;
        if (obj == null) {
            basicUserTokenEntity = this.basicUserTokenService.getByToken(token);
            CacheGenUtils.put("SYS_USER_TOKEN" + token, basicUserTokenEntity);
        } else {
            basicUserTokenEntity = (BasicUserTokenEntity) obj;
        }

        return basicUserTokenEntity;
    }

    public BasicUserEntity getUser(Long userId) {
        Object obj = CacheGenUtils.get("SYS_USER" + userId);
        BasicUserEntity basicUserEntity;
        if (obj == null) {
            basicUserEntity = this.basicUserTokenService.selectUserEntityById(userId);
            String deptId = this.basicDeptService.getDeptIdByPkId(basicUserEntity.getDeptPkid());
            basicUserEntity.setB0110(deptId);
            basicUserEntity.setDeptId(deptId);
            CacheGenUtils.put("SYS_USER" + userId, basicUserEntity);
        } else {
            basicUserEntity = (BasicUserEntity) obj;
        }

        return basicUserEntity;
    }

    public UserDetail getUserDetail(Long userId) {
        Object obj = CacheGenUtils.get("SYS_USER_DETAIL" + userId);
        UserDetail userDetail;
        if (obj != null && ((UserDetail) obj).getId() != null) {
            userDetail = (UserDetail) obj;
        } else {
//            BasicUserEntity basicUserEntity = this.basicUserTokenService.selectUserEntityById(userId);
//            BasicDeptDTO deptDTO = this.basicDeptService.getDeptByPkId(basicUserEntity.getDeptPkid());
//            userDetail = (UserDetail) ConvertUtils.sourceToTarget(basicUserEntity, UserDetail.class);
//            userDetail.setDeptPkid(deptDTO.getPkid());
//            userDetail.setB0110(deptDTO.getId());
//            userDetail.setDeptName(deptDTO.getName());
//            userDetail.setUsername(basicUserEntity.getUsername());
//            userDetail.setName(basicUserEntity.getUsername());
//            String companyPkid = basicUserEntity.getCompanyPkid();
//            if (StringUtils.isEmpty(companyPkid)) {
//                companyPkid = userDetail.getDeptPkid();
//            }
//
//            deptDTO = this.basicDeptService.getDeptByPkId(companyPkid);
//            userDetail.setCompanyId(deptDTO.getId());
//            userDetail.setCompanyPkid(deptDTO.getPkid());
//            userDetail.setCompanyName(deptDTO.getName());
//            List<UserCompany> userCompanies = this.basicUserDeptService.selectUserCompany(basicUserEntity.getId());
//            userDetail.setCompanies(userCompanies);
//            List<RoleBusiness> roleBusinessList = this.basicUserRoleBusinessService
//                    .selectRoleBusinessList(basicUserEntity.getId());
//            CacheGenUtils.put("SYS_USER_DETAIL" + userId, userDetail);

            BasicUserEntity basicUserEntity = this.basicUserTokenService.selectUserEntityById(userId);
            ZhjdSysDeptEntity deptDTO = zhjdSysDeptService.getById(basicUserEntity.getDeptPkid());
            userDetail = (UserDetail) ConvertUtils.sourceToTarget(basicUserEntity, UserDetail.class);
            userDetail.setUsername(basicUserEntity.getUsername());
            userDetail.setName(basicUserEntity.getUsername());
            userDetail.setDeptPkid(deptDTO.getPkid());
            userDetail.setB0110(deptDTO.getId());
            userDetail.setDeptName(deptDTO.getName());
            userDetail.setCompanyId(deptDTO.getId());
            userDetail.setCompanyPkid(deptDTO.getPkid());
            userDetail.setCompanyName(deptDTO.getName());

            List<UserCompany> userCompanies = this.basicUserDeptService.selectUserCompany(basicUserEntity.getId());
            userDetail.setCompanies(userCompanies);

            // 判断用户可查询数据范围
            // 如果是基地用户，只能查询本基地数据
            // 如果不是基地用户，可以查询所有基地数据
            String type = deptDTO.getType();
            List<String> baseCodes = new ArrayList<String>();
            if (ZhjdSysDeptConstants.TYPE_BASE.equals(type)) {
                baseCodes.add(deptDTO.getCode());
            } else {
                List<ZhjdSysDeptEntity> baseList = zhjdSysDeptService.getAllBase();
                if (null != baseList) {
                    baseList.forEach(item -> {
                        baseCodes.add(item.getCode());
                    });
                }
            }
            userDetail.setDeptIdList(baseCodes);

            CacheGenUtils.put("SYS_USER_DETAIL" + userId, userDetail);
        }

        return userDetail;
    }

    public UserDetail setUserDetail(Long userId, String currCompanyPkid) {
        BasicUserEntity basicUserEntity = this.basicUserTokenService.selectUserEntityById(userId);
        UserDetail userDetail = (UserDetail) ConvertUtils.sourceToTarget(basicUserEntity, UserDetail.class);
        BasicDeptDTO deptDTO = this.basicUserDeptService.getUserDeptByCompanyPkid(userId, currCompanyPkid);
        userDetail.setDeptPkid(deptDTO.getPkid());
        userDetail.setB0110(deptDTO.getId());
        userDetail.setDeptName(deptDTO.getName());
        userDetail.setUsername(basicUserEntity.getUsername());
        userDetail.setName(basicUserEntity.getUsername());
        deptDTO = this.basicDeptService.getDeptByPkId(currCompanyPkid);
        userDetail.setCompanyId(deptDTO.getId());
        userDetail.setCompanyPkid(deptDTO.getPkid());
        userDetail.setCompanyName(deptDTO.getName());
        List<UserCompany> userCompanies = this.basicUserDeptService.selectUserCompany(basicUserEntity.getId());
        userDetail.setCompanies(userCompanies);
//        List<RoleBusiness> roleBusinessList = this.basicUserRoleBusinessService
//                .selectRoleBusinessList(basicUserEntity.getId());
        CacheGenUtils.put("SYS_USER_DETAIL" + userId, userDetail);
        return userDetail;
    }

    @Override
    public SysDeptEntity getCompany(String deptId) {
        // 超级管理员，部门ID为null
        if (deptId == null) {
            return null;
        }
        SysDeptEntity sysDeptEntity = sysDeptService.getByRealId(deptId);
        while (!"1".equals(sysDeptEntity.getType())) {
            if (StringUtils.isEmpty(sysDeptEntity.getPid())) {
                return null;
            }
            sysDeptEntity = sysDeptService.getByRealId(sysDeptEntity.getPid());
        }

        return sysDeptEntity;

    }

    @Override
    public SysDeptEntity getDept(String deptId) {
        return sysDeptService.getByRealId(deptId);
    }

}
